package com.ckd.flyingtrip.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.javabean.HomePayBean;
import com.ckd.flyingtrip.utils.CityTable;
import com.ckd.flyingtrip.utils.StringUtils;

import java.util.List;

public class HomePayAdapter extends RecyclerView.Adapter<HomePayAdapter.MyHolder> {

    private Context context;
    private List<HomePayBean.DataBean> dataBeans;

    public HomePayAdapter(Context context, List<HomePayBean.DataBean> dataBeans) {
        this.context = context;
        this.dataBeans = dataBeans;
    }

    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onClick(View view, int position, TextView textView);

        void onAddClick(View view, int position, TextView textView, TextView textView1);

        void onLessClick(View view, int position, TextView textView, TextView textView1);
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.item_home_pay_recyclerview, null));
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {

        String costType = "";
        String costRefundFlg = "";
        switch (dataBeans.get(position).getCostType()) {
            case "1":
                costType = "押金";
                costRefundFlg = "可退";
                holder.linearLayout.setVisibility(View.VISIBLE);
                holder.textView5.setVisibility(View.GONE);
                holder.textView6.setVisibility(View.GONE);
                break;
            case "2":
                costType = "租金";
                holder.linearLayout.setVisibility(View.GONE);
                holder.textView5.setVisibility(View.VISIBLE);
                holder.textView6.setVisibility(View.VISIBLE);
                switch (dataBeans.get(position).getCostRefundFlg()) {
                    case "0":
                        costRefundFlg = "不可退";
                        break;
                    case "1":
                        costRefundFlg = "可退";
                        break;
                }
                break;
        }

        String costTypeCategory = "";
        switch (dataBeans.get(position).getCostTypeCategory()) {
            case "11":
                costTypeCategory = "电池";
                holder.buy.setVisibility(View.VISIBLE);
                break;
            case "12":
                costTypeCategory = "电动车";
                holder.buy.setVisibility(View.GONE);
                holder.linearLayout.setVisibility(View.GONE);
                break;
            case "13":
                costTypeCategory = "通用";
                holder.buy.setVisibility(View.GONE);
                holder.linearLayout.setVisibility(View.GONE);
                break;
        }

        holder.textView1.setText("城 市 名 称 ：" + CityTable.getCityCode(dataBeans.get(position).getCityId()));
        holder.textView2.setText("费 用 类 型 ：" + costType);
        holder.textView3.setText("费 用 类 别 ：" + costTypeCategory);
        holder.textView4.setText("费 用 金 额 ：¥" + StringUtils.GetZero(dataBeans.get(position).getCostMoney()));
        holder.textView5.setText("使 用 天 数 ：" + dataBeans.get(position).getCostUseDays() + "天");
        holder.textView6.setText("赠 送 天 数 ：" + dataBeans.get(position).getCostGiveDays() + "天");
        holder.textView7.setText("是否可退款：" + costRefundFlg);
        holder.textView8.setText("费 用 描 述 ：" + dataBeans.get(position).getCostDesc());


        holder.buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(v, position, holder.num);
            }
        });

        holder.icon1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onLessClick(v, position, holder.num, holder.textView4);
            }
        });

        holder.icon2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onAddClick(v, position, holder.num, holder.textView4);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView textView1, textView2, textView3, textView4, textView5, textView6, textView7, textView8, buy, num;
        LinearLayout linearLayout;
        ImageView icon1, icon2;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            textView1 = itemView.findViewById(R.id.item_home_pay_recyclerview_textView1);
            textView2 = itemView.findViewById(R.id.item_home_pay_recyclerview_textView2);
            textView3 = itemView.findViewById(R.id.item_home_pay_recyclerview_textView3);
            textView4 = itemView.findViewById(R.id.item_home_pay_recyclerview_textView4);
            textView5 = itemView.findViewById(R.id.item_home_pay_recyclerview_textView5);
            textView6 = itemView.findViewById(R.id.item_home_pay_recyclerview_textView6);
            textView7 = itemView.findViewById(R.id.item_home_pay_recyclerview_textView7);
            textView8 = itemView.findViewById(R.id.item_home_pay_recyclerview_textView8);
            buy = itemView.findViewById(R.id.item_home_pay_recyclerview_buy);
            linearLayout = itemView.findViewById(R.id.item_home_pay_recyclerview_ll);
            icon1 = itemView.findViewById(R.id.item_home_pay_recyclerview_icon1);
            icon2 = itemView.findViewById(R.id.item_home_pay_recyclerview_icon2);
            num = itemView.findViewById(R.id.item_home_pay_recyclerview_num);
        }
    }
}
